﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Runtime.CompilerServices;
using Microsoft.Phone.BackgroundAudio;

namespace WP8DataModel.Model
{
    public class PlayerAgentDataContext : DataContext
    {
        // Pass the connection string to the base class.
        public PlayerAgentDataContext(string connectionString)
            : base(connectionString)
        { }

        // Specify a table for the to-do items.
        public Table<StreamDescription> Streams;

    }

    [Table]
    public class StreamDescription : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property, and database column.
        private int _id;

        //TODO: rewrite it to support cool .net4.5 feature of the attribute name
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get { return _id; }
            set
            {
                SetField(ref _id, value);
            }
        }

        private int _stationId;
        [Column]
        public int StationId
        {
            get { return _stationId; }
            set
            {
                SetField(ref _stationId, value);
            }
        }

        // Define item name: private field, public property, and database column.
        private string _uri;

        [Column]
        public string Uri
        {
            get { return _uri; }
            set
            {
                SetField(ref _uri, value);
            }
        }

        // Define item name: private field, public property, and database column.
        private string _shortName;

        [Column]
        public string ShortName
        {
            get { return _shortName; }
            set
            {
                SetField(ref _shortName, value);
            }
        }

        private string _description;

        [Column]
        public string Description
        {
            get { return _description; }
            set
            {
                SetField(ref _description, value);
            }
        }

        public AudioTrack AsAudioTrack()
        {
            return new AudioTrack(
                new Uri(Uri, UriKind.Absolute), 
                ShortName, 
                Description, 
                string.Empty, 
                null, 
                Convert.ToString(StationId), 
                EnabledPlayerControls.All);
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
        protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (EqualityComparer<T>.Default.Equals(field, value))
            {
                return false;
            }
            NotifyPropertyChanging(propertyName);
            field = value;
            NotifyPropertyChanged(propertyName);
            return true;
        }


        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }
}
